package person.learn.conf;

import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import person.learn.filter.ResourcesFilter;

/**
 * Created by barton on 16-1-29.
 */
@Configuration
public class WebConfig implements EmbeddedServletContainerCustomizer {

    /**
     * 为特定的资源添加一个过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean appFilter() {
        FilterRegistrationBean fgb = new FilterRegistrationBean();
        fgb.setFilter(new ResourcesFilter());

        // 当请求的URL中含有/images/是，会先经过filter处理
        fgb.addUrlPatterns("/images/*");
        return fgb;
    }

    /**
     * 实现自定义错误处理，
     *
     * @param container
     */
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
        container.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500"));
    }
}
